// 进入页面同时进行请求，二级分类display：none,点击哪一个哪一个display：block
// let mains = document.querySelector("#mains")


$.ajax({
    url: "http://chst.vip:1234/api/getbrandtitle"
}).then(res => {
    // console.log(res);
    let html = ""
    res.result.forEach(item => {
        html += `<div  class="panel-group"brandtitleid=${item.brandTitleId}>
        <div class="dv">
            <h4><a>
                    <span>${item.brandTitle}</span>
                    <em>▼</em>
                </a>
            </h4>
        </div>
        </div>
        <div style="display:none" class="panel-body">

         </div>
        `
        $.ajax({
            url: `http://chst.vip:1234/api/getbrand?brandtitleid=${item.brandTitleId}`,

        }).then(res => {
            // console.log(res);
            let rows = document.querySelectorAll(".panel-body")
            // console.log(rows);
            let rowr = ""
            res.result.forEach(item => {
                rowr += `<div brandName=${item.brandName} brandid=${item.brandId} class="row">
                <a>${item.brandName}
                <em>${item.brandId}</em>
                <div>${item.brandInfo}</div>
                </a>

                </div>`
            })
            rows[item.brandTitleId].innerHTML = rowr
            let rowre = document.querySelectorAll(".row")
            Array.prototype.slice.call(rowre).forEach(item => {

                item.onclick = function () {
                    let brandid = this.getAttribute('brandid')
                    let brandName = this.getAttribute('brandName')
                    location.href = `./brandTitle.html?title=${brandName}&brandid=${brandid}`

                }
            })



        })


    })
    mains.innerHTML = html
    let group = document.querySelectorAll(".panel-group")//获取每个点击元素,是数组类型，
    // console.log(group)
    let panelbody = document.querySelectorAll(".panel-body")
    // console.log(panelbody)
    Array.prototype.slice.call(group).forEach(item => {
        // console.log(item)
        item.onclick = function () {
            // panelbody[titleid].style.display = 'none'
            let groupDiv = this
            let brandtitleid = this.getAttribute("brandtitleid")

            // panelbody[titleid].style.display = 'none'
            // console.log(brandtitleid)
            this.isClick = !this.isClick
            if (this.isClick) {
                panelbody[brandtitleid].style.display = "block"
            } else {
                panelbody[brandtitleid].style.display = "none"
            }
        }
    })



})
